#include<iostream>
#include<vector>
#include<unordered_map>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
using namespace std;
typedef long long ll;
template<typename T> 
inline void read(T &x){
	x=0;T f=1;char ch;
	do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9');
	do x=x*10+ch-'0',ch=getchar();while(ch<='9'&&ch>='0');x*=f;
}

struct node{
  int a,b;
  bool operator<(const node&x)const{
    return b>x.b;
  }
};
void exgcd(ll a, ll b, ll& x, ll& y) {
  if (b == 0) {
    x = 1, y = 0;
    return;
  }
  exgcd(b, a % b, y, x);
  y -= a / b * x;
}
ll ny(ll a,ll q){
  ll x,y;
  exgcd(a,q,x,y);
  x=(x%q+q)%q;
  return x;
}
ll gcd(ll a,ll b){
  if(b==0)return a;
  return gcd(b,a%b);
}
ll qs(ll a,ll b,ll q){
  ll res=1;
  while(b){
    if(b&1)res=(res*a)%q;
    a=(a*a)%q;
    b>>=1;
  }
  return res;
}

ll fac[1000006];
const ll mod=1e9+7;
ll C(ll m,ll n){
  ll res=fac[n];
  res=(res*ny(fac[m],mod))%mod;
  res=(res*ny(fac[n-m],mod))%mod;
  return res;
}
ll dp[1000005];

//P4071 错排问题，排列组合
int main(){
    #ifdef BUG
    freopen("a.in","r",stdin);
    #endif
    int t;
    read(t);
    ll n,m;
    fac[0]=1;
    for(ll i=1;i<=1000000;i++)fac[i]=(fac[i-1]*i)%mod;
    dp[1]=0;
    dp[2]=dp[0]=1;
    for(int i=3;i<=1000000;i++)
      dp[i]=((i-1)*(dp[i-2]+dp[i-1]))%mod;
    
    while(t--){
      read(n);read(m);
      ll res=(C(m,n)*dp[n-m])%mod;
      printf("%lld\n",res);
    }
}
